﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NRemedy;
using NRemedy.Linq;

namespace CMDBImportCIDLL
{
    public class CommonDTShipOperate
    {
        public void SaveAllRelationShipDT(string server, string login, string password, Company company, List<RelationShipModel> list, ShowMessageDelegate show)
        {
            using (ARLoginContext context = new ARLoginContext(server, login, password))
            {
                foreach (var item in list)
                {
                    string classid = string.Empty;
                    string sou_ins = string.Empty;
                    string des_ins = string.Empty;
                    classid = getRelationshipClassId(item.RelationShipName);
                    item.RelationShipName = classid;
                    sou_ins = GetInstanceIdByCiName(context, item.SourceCIName, company.CompanyName);
                    des_ins = GetInstanceIdByCiName(context, item.DestinationCIName, company.CompanyName);
                    if (!checkshipExist(context, classid, DatasetId.BMCASSET, des_ins, sou_ins))
                    {
                        switch (classid.Trim())
                        {
                            case ClassId.BMC_DEPENDENCY:
                                SaveDependeny(context, item, company, login, show);
                                break;
                            case ClassId.BMC_BASERELATIONSHIP:
                                SaveBaseRelationship(context, item, company, login, show);
                                break;
                            case ClassId.BMC_COMPONENT:
                                SaveComponent(context, item, company, login, show);
                                break;
                            case ClassId.BMC_MEMBEROFCOLLECTION:
                                SaveMemberOf(context, item, company, login, show);
                                break;
                        }
                    }
                }
            }
        }

        private void SaveComponent(ARLoginContext context, RelationShipModel ship, Company company, string login, ShowMessageDelegate show)
        {
            BMCCOREBMC_Component component = new BMCCOREBMC_Component
            {
                Submitter = login,
                Short_Description = "COMPONENT",
                Name = "COMPONENT",
                ClassId = ClassId.BMC_COMPONENT,
                DatasetId = DatasetId.BMCASSET,
                DestinationInstanceId = GetInstanceIdByCiName(context, ship.DestinationCIName, company.CompanyName),
                SourceInstanceId = GetInstanceIdByCiName(context, ship.SourceCIName, company.CompanyName),
                CMDBWriteSecurity = string.Concat(";", company.CompanyPermission, ";", Permission.PUBLIC, ";"),
                HasImpact = BMCCOREBMC_BaseRelationship.HasImpact_Enum.Yes,
                ImpactDirection = BMCCOREBMC_BaseRelationship.ImpactDirection_Enum.SourceDestination,
            };
            ARProxy<BMCCOREBMC_Component> proxy = new ARProxy<BMCCOREBMC_Component>(context);
            proxy.CreateEntry(component);
            string meaasge = string.Concat("插入关系：", ship.SourceCIName, ship.RelationShipName, ship.DestinationCIName);
            show(meaasge);
        }

        private void SaveDependeny(ARLoginContext context, RelationShipModel ship, Company company, string login, ShowMessageDelegate show)
        {
            BMCCOREBMC_Dependency dependeny = new BMCCOREBMC_Dependency
            {
                Submitter = login,
                Short_Description = "DEPENDENCY",
                Name = "DEPENDENCY",
                ClassId = ClassId.BMC_DEPENDENCY,
                DatasetId = DatasetId.BMCASSET,
                DestinationInstanceId = GetInstanceIdByCiName(context, ship.DestinationCIName, company.CompanyName),
                SourceInstanceId = GetInstanceIdByCiName(context, ship.SourceCIName, company.CompanyName),
                CMDBWriteSecurity = string.Concat(";", company.CompanyPermission, ";", Permission.PUBLIC, ";"),
                HasImpact = BMCCOREBMC_BaseRelationship.HasImpact_Enum.Yes,
                ImpactDirection = BMCCOREBMC_BaseRelationship.ImpactDirection_Enum.SourceDestination,
            };
            ARProxy<BMCCOREBMC_Dependency> proxy = new ARProxy<BMCCOREBMC_Dependency>(context);
            proxy.CreateEntry(dependeny);
            string meaasge = string.Concat("插入关系：", ship.SourceCIName, ship.RelationShipName, ship.DestinationCIName);
            show(meaasge);
        }

        private void SaveMemberOf(ARLoginContext context, RelationShipModel ship, Company company, string login, ShowMessageDelegate show)
        {
            BMCCOREBMC_MemberOfCollection memberOf = new BMCCOREBMC_MemberOfCollection
            {
                Submitter = login,
                Short_Description = "MEMBEROFCOLLECTION",
                Name = "MEMBEROFCOLLECTION",
                ClassId = ClassId.BMC_MEMBEROFCOLLECTION,
                DatasetId = DatasetId.BMCASSET,
                DestinationInstanceId = GetInstanceIdByCiName(context, ship.DestinationCIName, company.CompanyName),
                SourceInstanceId = GetInstanceIdByCiName(context, ship.SourceCIName, company.CompanyName),
                CMDBWriteSecurity = string.Concat(";", company.CompanyPermission, ";", Permission.PUBLIC, ";"),
                HasImpact = BMCCOREBMC_BaseRelationship.HasImpact_Enum.No,
                ImpactDirection = BMCCOREBMC_BaseRelationship.ImpactDirection_Enum.Unknown,
            };
            ARProxy<BMCCOREBMC_MemberOfCollection> proxy = new ARProxy<BMCCOREBMC_MemberOfCollection>(context);
            proxy.CreateEntry(memberOf);
            string meaasge = string.Concat("插入关系：", ship.SourceCIName, ship.RelationShipName, ship.DestinationCIName);
            show(meaasge);
        }

        private void SaveBaseRelationship(ARLoginContext context, RelationShipModel ship, Company company, string login, ShowMessageDelegate show)
        {
            BMCCOREBMC_BaseRelationship impact = new BMCCOREBMC_BaseRelationship
            {
                Submitter = login,
                Short_Description = "BASERELATIONSHIP",
                Name = "BASERELATIONSHIP",
                ClassId = ClassId.BMC_BASERELATIONSHIP,
                DatasetId = DatasetId.BMCASSET,
                DestinationInstanceId = GetInstanceIdByCiName(context, ship.DestinationCIName, company.CompanyName),
                SourceInstanceId = GetInstanceIdByCiName(context, ship.SourceCIName, company.CompanyName),
                CMDBWriteSecurity = string.Concat(";", company.CompanyPermission, ";", Permission.PUBLIC, ";"),
                HasImpact = BMCCOREBMC_BaseRelationship.HasImpact_Enum.Yes,
                ImpactDirection = BMCCOREBMC_BaseRelationship.ImpactDirection_Enum.SourceDestination,
            };
            ARProxy<BMCCOREBMC_BaseRelationship> proxy = new ARProxy<BMCCOREBMC_BaseRelationship>(context);
            proxy.CreateEntry(impact);
            string meaasge = string.Concat("插入关系：", ship.SourceCIName, ship.RelationShipName, ship.DestinationCIName);
            show(meaasge);
        }

        private string GetInstanceIdByCiName(ARLoginContext context, string name, string company)
        {
            ARSet<BMCCOREBMC_BaseElement> aa = new ARSet<BMCCOREBMC_BaseElement>(context, new ARServerDefaultFactory());
            var q = from s in aa
                    where (s.DatasetId == DatasetId.BMCASSET) && (s.Name == name) && (s.Company == company)
                    select s;
            BMCCOREBMC_BaseElement item = q.ToList().FirstOrDefault();
            if (item != null)
            {
                return item.InstanceId;
            }
            else
            {
                return "";
            }
        }

        private bool checkshipExist(ARLoginContext context, string classId, string datasetId, string destination_InstanceId, string source_InstanceId)
        {

            ARSet<BMCCOREBMC_BaseRelationship> aa = new ARSet<BMCCOREBMC_BaseRelationship>(context, new ARServerDefaultFactory());
            var q = from s in aa
                    where (s.ClassId == classId) && (s.DatasetId == datasetId) && (s.DestinationInstanceId == destination_InstanceId) && (s.SourceInstanceId == source_InstanceId)
                    select s;
            return q.Count() > 0;

        }

        private string getRelationshipClassId(string shipname)
        {
            string str = string.Empty;
            switch (shipname.Trim())
            {
                case ShipType.DEPENDENCY:
                    str = ClassId.BMC_DEPENDENCY;
                    break;
                case ShipType.IMPCT:
                    str = ClassId.BMC_BASERELATIONSHIP;
                    break;
                case ShipType.COMPONENT:
                    str = ClassId.BMC_COMPONENT;
                    break;
                case ShipType.MEMBEROFCOLLECTION:
                    str = ClassId.BMC_MEMBEROFCOLLECTION;
                    break;
            }
            return str;
        }
    }
}
